package com.kyc.kycxkplaygame.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.kyc.kycxkplaygame.R;
import com.kyc.kycxkplaygame.dbutils.GetPropLevelUtils;
import com.kyc.kycxkplaygame.dbutils.IntImp.AlasbusyIApi;
import com.kyc.kycxkplaygame.dbutils.inter.HttpQuestResultListener;
import com.kyc.kycxkplaygame.entity.LevelCode;
import com.kyc.kycxkplaygame.entity.PropsCode;
import com.kyc.kycxkplaygame.entity.UserLevelBean;
import com.kyc.kycxkplaygame.greendao.LevelPassBean;
import com.kyc.kycxkplaygame.greendao.manager.LevelInfoManager;


public class ClearanceActivity extends Activity implements HttpQuestResultListener {

    private TextView level_name;
    private TextView clearan_txt;
    private Button btn_next;
    private String newLevel;
    //    private ImageView first;
    //    private ImageView two;
    //    private ImageView three;
    private LinearLayout tongguan_image;
    private String levle;
    //String pass_part_old = "";
    private int fraction;
    private int grape;
    private int fraction666;
    private int batter;
    private ImageView clearan_stars;
    private TextView grape_txt;
    private AlasbusyIApi alasbusyIApi;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setFinishOnTouchOutside(false);
        setContentView(R.layout.clearance_activity);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.alpha = 1f;
        window.setAttributes(layoutParams);

        Intent intent = getIntent();
        boolean clearance = intent.getBooleanExtra("clearance", false);
        levle = intent.getStringExtra("levle");
        fraction = Integer.valueOf(intent.getStringExtra("fraction"));
        grape = Integer.valueOf(intent.getStringExtra("grape"));
        fraction666 = Integer.valueOf(intent.getStringExtra("fraction666"));
        batter = Integer.valueOf(intent.getStringExtra("batter"));

        final int newlevel = Integer.parseInt(levle) + 1;
        if (String.valueOf(newlevel).length() == 1) {
            newLevel = "0" + newlevel;
        } else {
            newLevel = String.valueOf(newlevel);
        }

        level_name = (TextView) findViewById(R.id.level_name);
        level_name.setText("第" + levle + "关");

        clearan_txt = (TextView) findViewById(R.id.clearan_txt);
        tongguan_image = (LinearLayout) findViewById(R.id.tongguan_image);
        //        first = (ImageView) findViewById(first);
        //        two = (ImageView) findViewById(R.id.two);
        //        three = (ImageView) findViewById(R.id.three);

        clearan_stars = findViewById(R.id.clearan_stars);
        btn_next = (Button) findViewById(R.id.btn_next);

        grape_txt = findViewById(R.id.grape_txt);

        //String pass_start = (String) SharedPreferencesUtils.getParam(ClearanceActivity.this, "pass_start", "");

        alasbusyIApi = AlasbusyIApi.getInstance(this);
        alasbusyIApi.setHttpQuestResultListener(this);

        if (clearance) {
            clearan_txt.setTextColor(getResources().getColor(R.color.clearance));
            tongguan_image.setVisibility(View.VISIBLE);
            if (fraction == 1) {
                clearan_stars.setImageResource(R.drawable.one_stars);
                //                two.setImageResource(R.drawable.normal_fraction);
                //                three.setImageResource(R.drawable.normal_fraction);
            } else if (fraction == 2) {
                clearan_stars.setImageResource(R.drawable.two_stars);
                //                two.setImageResource(R.drawable.red_fraction);
                //                three.setImageResource(R.drawable.normal_fraction);
            } else if (fraction == 3) {
                clearan_stars.setImageResource(R.drawable.three_stars);
                //                two.setImageResource(R.drawable.red_fraction);
                //                three.setImageResource(R.drawable.red_fraction);
            }
            clearan_txt.setText("已通关");
           /* boolean map_istrue = (boolean) SharedPreferencesUtils.getParam(ClearanceActivity.this, "map_istrue", false);
            Map<String, UserInfoBean.UserBean.LevelBean> map = new HashMap<>();
            if (*//*Integer.parseInt(levle) > 1*//*map_istrue) {
                HashMap<String, UserInfoBean.UserBean.LevelBean> pass_status = SharedPreferencesUtils.getHashMapData(ClearanceActivity.this, "pass_status", PassStatusBean.class);
                if (pass_status != null) {
                    if (pass_status.size() > 0) {
                        map.putAll(pass_status);
                    }
                }
            }
            //map.put(levle, new UserInfoBean().UserBean.LevelBean(levle, String.valueOf(fraction)));
            SharedPreferencesUtils.putHashMapData(ClearanceActivity.this, "pass_status", map);
            SharedPreferencesUtils.setParam(ClearanceActivity.this, "map_istrue", true);*/

            String grape_currency = GetPropLevelUtils.getInstance().getPropNum("葡萄");
            int levelGrape = GetPropLevelUtils.getInstance().getPassLevelGrape(levle);
            Log.i("KYCC", "level == " + levelGrape + ", xianzai == " + grape);
            int passLevelStar = GetPropLevelUtils.getInstance().getPassLevelStar(levle);
            if(passLevelStar > 0) {
                if (levelGrape < grape) {
                    grape = grape - levelGrape;
                    GetPropLevelUtils.getInstance().updatePropNum("葡萄", grape + Integer.parseInt(grape_currency));
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"葡萄", grape + Integer.parseInt(grape_currency));
                } else if (levelGrape == grape) {
                    grape = levelGrape;
                    GetPropLevelUtils.getInstance().updatePropNum("葡萄", Integer.parseInt(grape_currency));
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"葡萄", Integer.parseInt(grape_currency));
                    grape = 0;
                } else {
                    grape = 0;
                    GetPropLevelUtils.getInstance().updatePropNum("葡萄", grape + Integer.parseInt(grape_currency));
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"葡萄", grape + Integer.parseInt(grape_currency));
                }
            }else {
                GetPropLevelUtils.getInstance().updatePropNum("葡萄", grape + Integer.parseInt(grape_currency));
                alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"葡萄", grape + Integer.parseInt(grape_currency));
            }
            grape_txt.setText(grape + "币");
            final LevelPassBean levelPassBean = new LevelPassBean();
            int infoCount = LevelInfoManager.getInstance().getLevelInfoCount();
            levelPassBean.setId(infoCount + 1);
            levelPassBean.setStatus("已通关");
            levelPassBean.setStarCount(fraction);
            levelPassBean.setLevel(levle);
            levelPassBean.setLianjicount(batter);
            levelPassBean.setPassfraction(fraction666);
            levelPassBean.setPutaoNum(grape);

            UserLevelBean userLevelBean = new UserLevelBean();
            userLevelBean.setLevel(Integer.parseInt(levle));
            userLevelBean.setBatter(batter);
            userLevelBean.setFraction(fraction666);
            userLevelBean.setGrepNum(grape);
            userLevelBean.setStartCount(fraction);
            userLevelBean.setUserID(GetPropLevelUtils.getInstance().getUserID());
            alasbusyIApi.insertLevel(userLevelBean);
            alasbusyIApi.updateUserLEvel(GetPropLevelUtils.getInstance().getUserID(),Integer.parseInt(newLevel));
            new Thread() {
                @Override
                public void run() {
                    LevelInfoManager.getInstance().insertOrUpdate(levelPassBean);
                    GetPropLevelUtils.getInstance().updateLevel(newLevel);
                }
            }.start();
            Log.i("KYCC", "newLevel   ====  " + newLevel);

            String tili_num = GetPropLevelUtils.getInstance().getPropNum("体力");
            alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"体力", Integer.parseInt(tili_num) + 1);
            GetPropLevelUtils.getInstance().updatePropNum("体力", Integer.parseInt(tili_num) + 1);


            btn_next.setText("下一关");
            btn_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int levelold = GetPropLevelUtils.getInstance().getLevel();
                    if (levelold < Integer.parseInt(newLevel)) {
                        // GetPropLevelUtils.getInstance().updatePropNum("体力",Integer.parseInt(newLevel));
                        GetPropLevelUtils.getInstance().updateLevel(newLevel);
                    }
                    String tili_num = GetPropLevelUtils.getInstance().getPropNum("体力");
                    if (Integer.parseInt(tili_num) > 0) {
                        Intent intent = new Intent(ClearanceActivity.this, GameActivity.class);
                        intent.putExtra("level_num", newLevel);
                        intent.putExtra("status", "tili");
                        setResult(300, intent);
                        ClearanceActivity.this.finish();
                    } else {
                        int xiyou = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("西柚"));//(int) SharedPreferencesUtils.getParam(ClearanceActivity.this, "xiyou", 0);
                        if (xiyou > 0) {
                            Intent intent = new Intent(ClearanceActivity.this, GameActivity.class);
                            intent.putExtra("level_num", newLevel);
                            intent.putExtra("status", "xiyou");
                            setResult(300, intent);
                            ClearanceActivity.this.finish();
                        }
                    }
                }
            });
        } else {
            clearan_txt.setTextColor(getResources().getColor(R.color.no_clearance));
            clearan_txt.setText("未通关");

            grape_txt.setText(grape + "币");

            alasbusyIApi.updateUserLEvel(GetPropLevelUtils.getInstance().getUserID(),Integer.parseInt(levle));
            GetPropLevelUtils.getInstance().updateLevel(levle);

            String tili_num = GetPropLevelUtils.getInstance().getPropNum("体力");
            alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"体力", Integer.parseInt(tili_num));
            GetPropLevelUtils.getInstance().updatePropNum("体力", Integer.parseInt(tili_num));

            tongguan_image.setVisibility(View.GONE);
            btn_next.setText("重玩本关");
            btn_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int levelold = GetPropLevelUtils.getInstance().getLevel();//(int) SharedPreferencesUtils.getParam(ClearanceActivity.this, "level", 1);
                    if (levelold < Integer.parseInt(levle)) {
                        // GetPropLevelUtils.getInstance().updatePropNum("体力",Integer.parseInt(levle));
                        alasbusyIApi.updateUserLEvel(GetPropLevelUtils.getInstance().getUserID(),Integer.parseInt(levle));
                        GetPropLevelUtils.getInstance().updateLevel(levle);
                    }
                    //String tili_num = (String) SharedPreferencesUtils.getParam(ClearanceActivity.this, "tili_num", "");
                    String tili_num = GetPropLevelUtils.getInstance().getPropNum("体力");
                    if (Integer.parseInt(tili_num) > 0) {
                        int newtili = Integer.parseInt(tili_num) - 1;
                        //SharedPreferencesUtils.setParam(ClearanceActivity.this, "tili_num", String.valueOf(newtili));
                        GetPropLevelUtils.getInstance().updatePropNum("体力", newtili);
                        Intent intent = new Intent(ClearanceActivity.this, GameActivity.class);
                        intent.putExtra("level_num", levle);
                        intent.putExtra("status", "tili");
                        setResult(300, intent);
                        ClearanceActivity.this.finish();
                    } else {
                        int xiyou = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("西柚"));
                        if (xiyou > 0) {
                            int newXiyou = xiyou - 1;
                            //SharedPreferencesUtils.setParam(ClearanceActivity.this,"xiyou",newXiyou);
                            GetPropLevelUtils.getInstance().updatePropNum("西柚", newXiyou);
                            Intent intent = new Intent(ClearanceActivity.this, GameActivity.class);
                            intent.putExtra("level_num", levle);
                            intent.putExtra("status", "xiyou");
                            setResult(300, intent);
                            ClearanceActivity.this.finish();
                        }
                    }
                }
            });
        }

        findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeBtn();
            }
        });

        findViewById(R.id.clerance_close_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeBtn();
            }
        });
    }

    @Override
    public void onBackPressed() {

    }

    public void closeBtn() {
        int levelold = GetPropLevelUtils.getInstance().getLevel();
        if (btn_next.getText().toString().equals("重玩本关")) {
            if (levelold < Integer.parseInt(levle)) {
                //SharedPreferencesUtils.setParam(ClearanceActivity.this, "level", Integer.parseInt(levle));
                alasbusyIApi.updateUserLEvel(GetPropLevelUtils.getInstance().getUserID(),Integer.parseInt(levle));
                GetPropLevelUtils.getInstance().updateLevel(levle);
            }
        } else if (btn_next.getText().toString().equals("下一关")) {
            if (levelold < Integer.parseInt(newLevel)) {
                //SharedPreferencesUtils.setParam(ClearanceActivity.this, "level", Integer.parseInt(newLevel));
                alasbusyIApi.updateUserLEvel(GetPropLevelUtils.getInstance().getUserID(),Integer.parseInt(newLevel));
                GetPropLevelUtils.getInstance().updateLevel(newLevel);
            }
            // Map<String, PassStatusBean> map = new HashMap<>();
            // HashMap<String, PassStatusBean> pass_status = SharedPreferencesUtils.getHashMapData(ClearanceActivity.this, "pass_status", PassStatusBean.class);
           /* if (pass_status.size() > 0) {
                map.putAll(pass_status);
            }*/
            //map.put(levle, new PassStatusBean(levle, String.valueOf(fraction)));
            //SharedPreferencesUtils.putHashMapData(ClearanceActivity.this, "pass_status", map);
        }
        LevelActivity.instanse.finish();
        startActivity(new Intent(ClearanceActivity.this, LevelActivity.class));
        ClearanceActivity.this.finish();
        GameActivity.instanse.finish();
    }

    @Override
    public void onError(Object e) {

    }

    @Override
    public void onSuccess(Object object) {
        if(object instanceof LevelCode) {
            LevelCode levelCode = (LevelCode) object;
            if(levelCode.getCode() == 200) {
                Log.i("KYCC", levelCode.getMessage());
            }
        }

        if(object instanceof PropsCode) {
            PropsCode propsCode = (PropsCode) object;
            Log.i("KYCC",propsCode.getMessage());
        }
    }

    @Override
    public void onQuestErroCode(Object object) {

    }
}
